package com.hycreate.platform.modules.atta;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.modules.atta.SfAttachment;
import com.hycreate.platform.modules.atta.SfAttachmentService;

/**
 * 附件Controller
 * @author hoader
 * @version 2017-01-10
 */
@Controller
@RequestMapping(value = "${adminPath}/atta/sfAttachment")
public class SfAttachmentController extends BaseController {

	@Autowired
	private SfAttachmentService sfAttachmentService;
	
	@ModelAttribute
	public SfAttachment get(@RequestParam(required=false) String id) {
		SfAttachment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sfAttachmentService.get(id);
		}
		if (entity == null){
			entity = new SfAttachment();
		}
		return entity;
	}
	
	@RequiresPermissions("atta:sfAttachment:view")
	@RequestMapping(value = {"list", ""})
	public String list(SfAttachment sfAttachment, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SfAttachment> page = sfAttachmentService.findPage(new Page<SfAttachment>(request, response), sfAttachment); 
		model.addAttribute("page", page);
		return "modules/atta/sfAttachmentList";
	}

	@RequiresPermissions("atta:sfAttachment:view")
	@RequestMapping(value = "form")
	public String form(SfAttachment sfAttachment, Model model) {
		model.addAttribute("sfAttachment", sfAttachment);
		return "modules/atta/sfAttachmentForm";
	}

	@RequiresPermissions("atta:sfAttachment:edit")
	@RequestMapping(value = "save")
	public String save(SfAttachment sfAttachment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sfAttachment)){
			return form(sfAttachment, model);
		}
		sfAttachmentService.save(sfAttachment);
		addMessage(redirectAttributes, "保存附件成功");
		return "redirect:"+Global.getAdminPath()+"/atta/sfAttachment/?repage";
	}
	
	@RequiresPermissions("atta:sfAttachment:edit")
	@RequestMapping(value = "delete")
	public String delete(SfAttachment sfAttachment, RedirectAttributes redirectAttributes) {
		sfAttachmentService.delete(sfAttachment);
		addMessage(redirectAttributes, "删除附件成功");
		return "redirect:"+Global.getAdminPath()+"/atta/sfAttachment/?repage";
	}

}